from rest_framework import serializers
from questions.models import Question, Reply
from users.models import User


class UserSerializerSimple(serializers.ModelSerializer):
    '''用户序列化器'''
    class Meta:
        model = User
        fields = [
                  'id',
                  'username',
                  'avatar'
                 ]


class ReplySerializerForSubAndParent(serializers.ModelSerializer):
    '''回答序列化器'''
    user = UserSerializerSimple(read_only=True)

    class Meta:
        model = Reply
        fields = [
                  "id",
                  "content",
                  "createtime",
                  "useful_count",
                  "unuseful_count",
                  "user"
                  ]

class ReplySerializerForList(serializers.ModelSerializer):
    '''回答列表序列化器'''
    user = UserSerializerSimple(read_only=True)
    subs = ReplySerializerForSubAndParent(read_only=True, many=True)
    parent = ReplySerializerForSubAndParent(read_only=True)

    class Meta:
        model = Reply
        fields = [
                  "id",
                  "content",
                  "createtime",
                  "useful_count",
                  'problem',
                  "unuseful_count",
                  "subs",
                  "user",
                  "parent"
                  ]



class QuestionSerializerForList(serializers.ModelSerializer):
    '''问答序列化器'''
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True,many=True)

    class Meta:
        model = Question
        fields = ["id",
                  "createtime",
                  "labels",
                  "reply",
                  "replyname",
                  "replytime",
                  "title",
                  "unuseful_count",
                  "useful_count",
                  "user","visits"
                  ]


class QuestionSerializerForCreate(serializers.ModelSerializer):
    '''发布问题序列化器'''
    class Meta:
        model = Question
        fields = "__all__"


class QuestionSerializerForDetail(serializers.ModelSerializer):
    '''问题详情序列化器'''
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)

    comment_question = ReplySerializerForList(read_only=True, many=True)
    comment_reply = ReplySerializerForList(read_only=True, many=True)
    answer_question = ReplySerializerForList(read_only=True, many=True)

    class Meta:
        model = Question
        fields = ["id",
                  "createtime",
                  "labels",
                  "reply",
                  "replyname",
                  "replytime",
                  "title",
                  "unuseful_count",
                  "useful_count",
                  "user",
                  "visits",
                  "content",
                  "comment_question",
                  "comment_reply",
                  "answer_question"
                  ]


class ReplySerializerForCreate(serializers.ModelSerializer):

    class Meta:
        model = Reply
        fields = '__all__'